(function () {
    angular.module('vehicleCheckApp').controller('ChecklistCtrl', ChecklistCtrl);
    function ChecklistCtrl($scope, $state, $ionicPopup, _DBSerive,$timeout, checkInfoService) {
        var checkInfo = checkInfoService.getInfo();
        console.log("========checkInfo==========");
		console.log(checkInfo);
		if(checkInfo.carType){
			console.log(checkInfo.carType.id);
			_DBSerive.queryCheckItem(checkInfo.carType.id).then(function(results){
				var checkItems = [];
				for(var i=0; i<results.rows.length; i++){
					var item = results.rows.item(i);
					item.photos = [];
					checkItems.push(item);
				}
				$scope.checkItems = checkItems;
                console.log("=========checkItems==========");
				console.log($scope.checkItems);
			});
		}


        $scope.takePhoto = function(index){
            console.log(index);
            //拍照
            navigator.camera.getPicture(function(imageURI) {
                $scope.$apply(function(){
                    $scope.checkItems[index].photos.push(imageURI);
                });
            }, function(errMsg) {
                console.log(errMsg);
            },{
                quality: 75,
                destinationType: Camera.DestinationType.FILE_URI
            });

        }
		
		//验证是否每项都检查了
        $scope.validate = function() {
			var notChenk = false;
			var notChenkIds = []; 
			var notPhoto = false;
			var notPhotoIds = []; 
			angular.forEach($scope.checkItems, function(checkItem){
				checkItem.warn = "";
				if(checkItem.answer){
					console.log(checkItem.photos.length);
					if(checkItem.answer == 2 && checkItem.photos.length == 0){
						notPhoto = true;
						notPhotoIds.push(checkItem.questionno);
						checkItem.warn = "red-text";
					}					
				}else{
					notChenk = true;
					notChenkIds.push(checkItem.questionno);
					checkItem.warn = "red-text";
				}
			});
			if(notChenk || notPhoto){
				var template = "";
				if(notChenk){
					template += "<div>第";
					angular.forEach(notChenkIds,function(id,i){
						template += id;
						if(i < notChenkIds.length -1)
							template += "、"
					});
					template += "题未检查，请选择\"是\"或\"否\"，</div>";
				}
				if(notPhoto){
					template += "<div>第";
					angular.forEach(notPhotoIds,function(id,i){
						template += id;
						if(i < notPhotoIds.length -1)
							template += "、"
					});
					template += "题未拍照，</div>";
				}
				template += "<div>请填写完整后再点击\"下一步\"。</div>";
				var alertPopup = $ionicPopup.alert({
					title: '检查未完成',
					cssClass:'myPopup',
					template: template,
					okText:'继续填写',
					okType:'button button-block button-positive next-btn'
				});
				alertPopup.then(function(res){
					//console.log(res);
				});
				return false;
			}
			return true;
        };
		
		//保存到本地
		var saveNotSubmitLists = function(){
			if($scope.validate()){
                //从websql获取notSubmitLists
                _DBSerive.queryNotSubmitList(localStorage.actno).then(function(result){
                    checkInfo.checkItems = $scope.checkItems;
                    var data = {};
                    data.actno = localStorage.actno;
                    var notSubmitLists = result.rows.length > 0 ? JSON.parse(result.rows.item(0).notSubmitLists) : [];
                    notSubmitLists.unshift(checkInfo);
                    data.notSubmitLists = notSubmitLists;
                    _DBSerive.insertNotSubmitList(data);
                });
                return true;
			}else{
				return false;
			}		
		}
		
		//保存后跳回首页
		$scope.saveBtn = function(){
			if(saveNotSubmitLists())
                $timeout(function(){
                    $state.go("vehicleCheck");
                },1000);
		}
		
		$scope.checklistSubmit= function(){
			if(saveNotSubmitLists())
                $timeout(function(){
                    $state.go('checklistPreview',{revise:false, index:0});
                },1000);

        }
    };
})();